package com.swinw.asumall.order.web;

import com.swinw.asumall.order.service.OrderService;
import com.swinw.asumall.order.vo.OrderConfirmVo;
import com.swinw.asumall.order.vo.OrderSubmitVo;
import com.swinw.asumall.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.ExecutionException;

@Controller
public class WebOrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/{page}.html")
    public String toPage(@PathVariable("page") String page){
        return page;
    }

    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        model.addAttribute("data",orderConfirmVo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo,Model model) throws Exception {
        //1、下单
        SubmitOrderResponseVo submitOrder = orderService.submitOrder(vo);
        //2、下单成功到支付选择页
        Integer code = submitOrder.getCode();
        if(code == null || code != 1){
            model.addAttribute("data",submitOrder);
            return "pay";
        }else{ //3、下单失败回到订单确认页
            return "redirect:http://order.swinw.com/toTrade";
        }
    }

}
